/************************************************************************************/
/*  Program name CAPAPIMP_AE_LINES.sql                                              */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_AE_LINES';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_AE_LINES;

	lv_Next_Seq_ID	NUMBER;
	lv_COA_ID		NUMBER;
	lv_Vend_ID		NUMBER;
	lv_Source_ID	NUMBER;
	lv_Ref2_ID		NUMBER;
	lv_Ref3_ID		NUMBER;
	lv_Ref9_ID		NUMBER;

BEGIN

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	AP_AE_LINES_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_AP_AE_LINES_MAP VALUES
			(
			C1.AE_LINE_ID,
			C1.AE_HEADER_ID,
			C1.AE_LINE_NUMBER,
			lv_Next_Seq_ID,
			CAPBRIMP_MAPPING.GetAEHeaderIDMapping(C1.AE_HEADER_ID),
			C1.AE_LINE_NUMBER
			--C1.AE_LINE_NUMBER,
			);

			lv_COA_ID	:=	CAPBRIMP_COMMON.GetCOAID(C1.SOB_SHORT_NAME);
			lv_Vend_ID	:=	CAPBRIMP_COMMON.GetVendorID_Name(C1.POVENDOR_NAME);

			IF		C1.SOURCE_TABLE	=	'AP_INVOICES'				THEN
				lv_Source_ID	:=	CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(C1.SOURCE_ID);
				lv_Ref9_ID		:=	NULL;
			ELSIF	C1.SOURCE_TABLE	=	'AP_INVOICE_DISTRIBUTIONS'	THEN
				lv_Source_ID	:=	CAPBRIMP_MAPPING.GetAPDistIDMapping(C1.SOURCE_ID);
				lv_Ref9_ID		:=	NULL;
			ELSIF	C1.SOURCE_TABLE	=	'AP_CHECKS'					THEN
				lv_Source_ID	:=	CAPBRIMP_MAPPING.GetCheckIDMapping(C1.SOURCE_ID);
				lv_Ref9_ID		:=	NULL;
			ELSIF	C1.SOURCE_TABLE	=	'AP_INVOICE_PAYMENTS'		THEN
				lv_Source_ID	:=	CAPBRIMP_MAPPING.GetInvPaymentIDMapping(C1.SOURCE_ID);
				lv_Ref9_ID		:=	lv_Source_ID;
			ELSE
				lv_Source_ID	:=	NULL;
				lv_Ref9_ID		:=	NULL;
			END IF;

			IF	(	C1.SOURCE_TABLE	=	'AP_INVOICES'				OR
					C1.SOURCE_TABLE	=	'AP_INVOICE_DISTRIBUTIONS'	OR
					C1.SOURCE_TABLE	=	'AP_INVOICE_PAYMENTS'			)	THEN
				lv_Ref2_ID	:=	CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(TO_NUMBER(C1.REFERENCE2));
			ELSE
				lv_Ref2_ID	:=	NULL;
			END IF;

			IF	(	C1.SOURCE_TABLE	=	'AP_CHECKS'					OR
					C1.SOURCE_TABLE	=	'AP_INVOICE_PAYMENTS'			)	THEN
				lv_Ref3_ID	:=	CAPBRIMP_MAPPING.GetCheckIDMapping(TO_NUMBER(C1.REFERENCE3));
			ELSE
				lv_Ref3_ID	:=	NULL;
			END IF;

			INSERT INTO AP_AE_LINES_ALL
			VALUES
			(
			lv_Next_Seq_ID,											--AE_LINE_ID,
			CAPBRIMP_MAPPING.GetAEHeaderIDMapping(C1.AE_HEADER_ID),	--AE_HEADER_ID,
			C1.AE_LINE_NUMBER,
			C1.AE_LINE_TYPE_CODE,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.CCID_D),		--CODE_COMBINATION_ID,
			C1.CURRENCY_CODE,
			C1.CURRENCY_CONVERSION_TYPE,
			C1.CURRENCY_CONVERSION_DATE,
			C1.CURRENCY_CONVERSION_RATE,
			C1.ENTERED_DR,
			C1.ENTERED_CR,
			C1.ACCOUNTED_DR,
			C1.ACCOUNTED_CR,
			C1.SOURCE_TABLE,
			lv_Source_ID,											--SOURCE_ID,
			CAPBRIMP_MAPPING.GetAPDistIDMapping(C1.ACCOUNT_OVERLAY_SOURCE_ID),	--ACCOUNT_OVERLAY_SOURCE_ID
			C1.GL_SL_LINK_ID,
			C1.DESCRIPTION,
			C1.ACCOUNTING_ERROR_CODE,
			C1.GL_TRANSFER_ERROR_CODE,
			lv_Vend_ID,															--THIRD_PARTY_ID,
			CAPBRIMP_COMMON.GetVendorSiteID(lv_Vend_ID, C1.ORG_ID, C1.POVENDORSITE_NAME),	--THIRD_PARTY_SUB_ID,
			C1.STAT_AMOUNT,
			C1.SUBLEDGER_DOC_SEQUENCE_ID,
			C1.SUBLEDGER_DOC_SEQUENCE_VALUE,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),					--ORG_ID,
			C1.CREATION_DATE,
			C1.USSGL_TRANSACTION_CODE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATED_BY,
			C1.LAST_UPDATE_LOGIN,
			C1.PROGRAM_UPDATE_DATE,
			C1.PROGRAM_APPLICATION_ID,
			C1.PROGRAM_ID,
			C1.REQUEST_ID,
			C1.REFERENCE1,
			TO_CHAR(lv_Ref2_ID),									--REFERENCE2,
			TO_CHAR(lv_Ref3_ID),									--REFERENCE3,
			C1.REFERENCE4,
			C1.REFERENCE5,
			C1.REFERENCE6,
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),			--REFERENCE7,
			C1.REFERENCE8,
			TO_CHAR(lv_Ref9_ID),									--REFERENCE9,
			C1.REFERENCE10,
			C1.APPLIED_FROM_TRX_HDR_TABLE,
			C1.APPLIED_FROM_TRX_HDR_ID,
			C1.APPLIED_TO_TRX_HDR_TABLE,
			C1.APPLIED_TO_TRX_HDR_ID,
			C1.TAX_CODE_ID,
			C1.TAXABLE_ENTERED_DR,
			C1.TAXABLE_ENTERED_CR,
			C1.TAXABLE_ACCOUNTED_DR,
			C1.TAXABLE_ACCOUNTED_CR,
			C1.TAX_LINK_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
